package com.tuozixuan.factory.simple2;

import java.util.HashMap;
import java.util.Map;

/**
 * 简单工厂模式-使用反射机制进行类注册的简单工厂模式<br/>
 * 使用反射机制注册产品类对象和实例化<br/><br/>
 * 
 * 缺点：反射机制也会降低程序的运行效率，在对性能要求很高的场景下应该避免使用这种机制
 * 
 * @author tuozixuan
 *
 */
public class VehicleFactory {
	
	private Map<String, Class> registeredProducts = new HashMap<>();

	public enum VehicleType {
		Bike, Car, Truck;
	}
	
	public void registerVehicle(String vehicleId, Class vehicleClass) {
		registeredProducts.put(vehicleId, vehicleClass);
	}
	
	public Vehicle createVehicle(String type) throws InstantiationException, IllegalAccessException {
		Class productClass = registeredProducts.get(type);
		return (Vehicle)productClass.newInstance();
	}
}
